package utils

import (
	"encoding/base64"
	"math/rand"
	"time"

	"github.com/bugfan/to"
)

func EncodeStr(cstr string) string {
	strbytes := []byte(cstr + to.String(time.Now().UnixNano()))
	midStr := base64.StdEncoding.EncodeToString(strbytes)
	midStr = midStr[0:5] + "Z" + midStr[5:]
	return base64.StdEncoding.EncodeToString([]byte(midStr))
}
func DecodeStr(str string) string {
	decoded, _ := base64.StdEncoding.DecodeString(str)
	midStr := string(decoded)
	midStr = midStr[0:5] + midStr[6:]
	decodedStr, _ := base64.StdEncoding.DecodeString(midStr)
	return to.String(decodedStr)
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func RandStringRunes(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letterRunes[rand.Intn(len(letterRunes))]
	}
	return string(b)
}
